package com.mmg.spzx.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mmg.spzx.manager.mapper.SysRoleMapper;
import com.mmg.spzx.manager.mapper.SysRoleUserMapper;
import com.mmg.spzx.manager.service.SysRoleService;
import com.mmg.spzx.model.dto.system.SysRoleDto;
import com.mmg.spzx.model.entity.system.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Override
    public PageInfo<SysRole> findByPage(Integer page, Integer limit, SysRoleDto sysRoleDto) {
        PageHelper.startPage(page, limit);
        List<SysRole> sysRoleList = sysRoleMapper.findByPage(sysRoleDto);
        return new PageInfo<>(sysRoleList);
    }

    @Override
    public void saveSysRole(SysRole sysRole) {
        sysRoleMapper.saveSysRole(sysRole);
    }

    @Override
    public void updateSysRole(SysRole sysRole) {
        sysRoleMapper.updateSysRole(sysRole);
    }

    @Override
    public SysRole findSysRoleById(Integer id) {
        return sysRoleMapper.findSysRoleById(id);
    }

    @Override
    public void deleteSysRole(Integer id) {
        sysRoleMapper.deleteSysRole(id);
    }

    @Override
    public Map<String, Object> findAllRoles(Long userId) {
        //查询所有角色
        List<SysRole> roleList = sysRoleMapper.findAllRoles();
        //根据用户id查询分配过的角色列表
        List<Long> roleIdList = sysRoleUserMapper.findRoleIdsByUserId(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("allRolesList", roleList);
        map.put("sysUserRoles", roleIdList);
        return map;
    }
}
